package com.sunday.aomicaichang.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.sunday.common.utils.ToastUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.constValues.ConstValue;
import com.sunday.aomicaichang.ui.order.PaySuccessActivity;
import com.sunday.aomicaichang.ui.recharge.RechargeSuccessActivity;
import com.sunday.member.entity.order.OrderItem;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.math.RoundingMode;

/**
 * Created by 刘涛 on 2017/6/14.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
    private OrderItem order;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transparent);
        api = WXAPIFactory.createWXAPI(this, ConstValue.APP_ID);
        api.handleIntent(getIntent(), this);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        switch (resp.errCode) {
            case 0:
                order=BaseApp.getInstance().getOrder();
                Intent intent = null;
                if (order.getType()==1) {
                    intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);//支付成功界面
                    intent.putExtra("totalMoney", String.format("￥%s", order.getPayAmount().setScale(2, RoundingMode.HALF_UP)));
                    intent.putExtra("address", order.getReceiveAddress());
                    intent.putExtra("name", order.getReceiveName());
                    intent.putExtra("mobile", order.getReceiveMobile());
                } else {
                    intent = new Intent(WXPayEntryActivity.this, RechargeSuccessActivity.class);//支付成功界面
                    intent.putExtra("totalMoney", String.format("￥%s", order.getPayAmount().setScale(2, RoundingMode.HALF_UP)));
                }
                startActivity(intent);
                finish();
                break;
            case -1:
                ToastUtils.showToast(WXPayEntryActivity.this, "交易失败,请重试");
                finish();
                break;
            case -2:
                ToastUtils.showToast(WXPayEntryActivity.this, "交易取消");
                finish();
                break;
        }

    }
}
